package com.ecoolex.union.pay.retailer.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.union.pay.retailer.core.dao.UnionPayPopularMerchantDAO;
import com.ecoolex.union.pay.retailer.core.dao.UnionPayPopularMerchantProductDAO;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchant;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantExample;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantProduct;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantProductExample;
import com.ecoolex.union.pay.retailer.pojo.UnionPayPopularMerchantDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UnionPayPopularMerchantManager {
    @Autowired
    private UnionPayPopularMerchantDAO dao;
    @Autowired
    private UnionPayPopularMerchantProductDAO productDao;
    @Autowired
    private UnionPayPopularMerchantProductManager unionPayPopularMerchantProductManager;

    /**
     * 获取热门商户表
     * @return
     */
    public List<UnionPayPopularMerchantDTO> getPopularMerchantList() {
        UnionPayPopularMerchantExample merchantExample = new UnionPayPopularMerchantExample();
        UnionPayPopularMerchantExample.Criteria merchantCriteria = merchantExample.createCriteria();
        merchantCriteria.andDeletedEqualTo(UnionPayPopularMerchant.NOT_DELETED);
        List<UnionPayPopularMerchant> merchants = dao.selectByExample(merchantExample);
        List<UnionPayPopularMerchantDTO> popularMerchants = merchants.stream().map(merchant ->{
            UnionPayPopularMerchantDTO dto = new UnionPayPopularMerchantDTO();
            BeanUtils.copyProperties(merchant,dto);
            boolean hasPopularProduct = Check.notNullAndGtZero(computingMerchantPopularProduct(merchant.getMerchantId()));
            dto.setHasHotProduct(Boolean.toString(hasPopularProduct));
            return dto;
        }).collect(Collectors.toList());
        return popularMerchants;
    }

    private long computingMerchantPopularProduct(String merchantId){
        UnionPayPopularMerchantProductExample productExample = new UnionPayPopularMerchantProductExample();
        UnionPayPopularMerchantProductExample.Criteria productCriteria = productExample.createCriteria();
        productCriteria.andMerchantIdEqualTo(merchantId)
        .andDeletedEqualTo(UnionPayPopularMerchantProduct.NOT_DELETED);
       return productDao.countByExample(productExample);
    }

   public UnionPayPopularMerchant addMerchant(UnionPayPopularMerchant record, String createUser){
	    UnionPayPopularMerchant old = getByMerchantId(record.getMerchantId());
	    BizAssert.isTrue(Check.isNull(old),ResultCode.VERIFY.build(10), "商户Id已经存在");
        record.setDeleted(UnionPayPopularMerchant.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }

    public UnionPayPopularMerchant get(Integer id) {
        return dao.selectByPrimaryKeyWithLogicalDelete(id, UnionPayPopularMerchant.NOT_DELETED);
    }
    
    public UnionPayPopularMerchant getByMerchantId(String merchantId) {
    	UnionPayPopularMerchantExample example = new UnionPayPopularMerchantExample();
        UnionPayPopularMerchantExample.Criteria criteria = example.createCriteria();
        criteria.andMerchantIdEqualTo(merchantId).andDeletedEqualTo(UnionPayPopularMerchant.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

    @Transactional
    public UnionPayPopularMerchant editMerchant(UnionPayPopularMerchant record,String oldMerchantId, String updateUser){
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, UnionPayPopularMerchant.Column.excludes(
                UnionPayPopularMerchant.Column.deleted,
                UnionPayPopularMerchant.Column.createUser,
                UnionPayPopularMerchant.Column.createTime));
        //更新关联的热门产品
        List<UnionPayPopularMerchantProduct> popularProductList = unionPayPopularMerchantProductManager.getPopularProduct(oldMerchantId);
        if(Check.notNull(popularProductList)) {
        	for (UnionPayPopularMerchantProduct popularProduct : popularProductList) {
            	popularProduct.setMerchantId(record.getMerchantId());
            	unionPayPopularMerchantProductManager.editMerchantProduct(popularProduct, updateUser);
    		}
        }
        return record;
    }

    public Pagination<UnionPayPopularMerchant> queryMerchant(String keyword, Page page) {
        UnionPayPopularMerchantExample example = new UnionPayPopularMerchantExample();
        UnionPayPopularMerchantExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(UnionPayPopularMerchant.NOT_DELETED);
        if (Check.notNullOrEmpty(keyword)){
            criteria.andMerchantNameLike("%"+keyword+"%");
        }
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        return Pagination.create(page,
                dao.countByExample(example),
                () -> dao.selectByExample(example));
    }

    public Pagination<UnionPayPopularMerchant> searchMerchant(String keyword, Page page) {
        UnionPayPopularMerchantExample example = new UnionPayPopularMerchantExample();
        UnionPayPopularMerchantExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(UnionPayPopularMerchant.NOT_DELETED);
        criteria.andMerchantNameLike("%"+keyword+"%");
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        return Pagination.create(page,
                dao.countByExample(example),
                () -> dao.selectByExample(example));
    }

    public void delete(UnionPayPopularMerchant merchant, String userName) {
        merchant.setDeleted(UnionPayPopularMerchant.IS_DELETED);
        merchant.setUpdateUser(userName);
        merchant.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(merchant, UnionPayPopularMerchant.Column.deleted, UnionPayPopularMerchant.Column.updateUser, UnionPayPopularMerchant.Column.updateTime);
    }
}
